package com.explorati.LeetCode090.subsetsWithDup;

import com.sun.jmx.remote.internal.ArrayQueue;

import java.util.*;

/**
 * @ Author : Weijian_Wang
 * @ Date : Created in 20:07 2020/10/5 0005
 * @ Description ：
 */
public class Solution1 {

    /**
     * 给定一个可能包含重复元素的整数数组 nums，返回该数组所有可能的子集（幂集）。
     * @param nums
     * @return
     */
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {

        Arrays.sort(nums);
        findSubsets(nums, 0, new ArrayDeque());

        return res;
    }

    private void findSubsets(int[] nums, int index, Deque preList) {
        res.add(new ArrayList<>(preList));

        for(int i = index; i < nums.length; i ++) {
            //index是用来进行同层元素的控制的
            if( i > index && nums[i] == nums[i - 1]) {
                continue;
            }
            preList.add(nums[i]);
            findSubsets(nums, i + 1, preList);
            preList.removeLast();
        }
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 2};
        new Solution1().subsetsWithDup(nums);
    }
}
